﻿using CloudStorageLight.Core;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Auth.OAuth2.Requests;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Drive.v2;
using Google.Apis.Json;
using Google.Apis.Util.Store;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace CloudStorageLight.Core
{
    [ExcludeFromCodeCoverage]
    public class GoogleDriveUtil
    {
        public static string GoogleDriveClientId { get { return SystemSettings.Instance.GoogleDriveClientId; } }
        public static string GoogleDriveClientSecret { get { return SystemSettings.Instance.GoogleDriveClientSecret; } }

        public static async Task<Dictionary<string, object>> AuthorizeAsync(string path, string callbackUrl, string returnUrl, HttpContextBase ctx)
        {
            var auth = new SessionAppAuthorizationCodeWebApp(path, callbackUrl, returnUrl, ctx);
            var result = await auth.AuthorizeAsync(CancellationToken.None);
            var dict = new Dictionary<string, object>();
            if (result.Credential != null)
            {
                dict["AccessToken"] = result.Credential.Token.AccessToken;
                dict["RefreshToken"] = result.Credential.Token.RefreshToken;
            }
            dict["RedirectUri"] = result.RedirectUri;
            return dict;
        }

    }
}
